﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RMS.Manage.Web.Controllers;
using RMS.BLL;
using RMS.EF.Model;
using RMS.EF.Model.Common;
using RMS.Common;
using System.Configuration;

namespace RMS.Manage.Web.Areas.RMS.Controllers
{
    [RMSAuthorize]
    public class ConfigController : BaseController
    {
        ConfigBll bll = new ConfigBll();
        /// <summary>
        /// 列表页
        /// </summary>
        /// <returns></returns>
        public ActionResult List()
        {
            #region 创建缓存依赖文件，用于更新缓存
            //var rely = ConfigurationManager.AppSettings["Rely_RMS_Config"];
            //FileHelper.CheckFilePath(rely);
            #endregion
            return View();
        }
        /// <summary>
        /// 列表分页查询
        /// </summary>
        /// <param name="searchValue">搜索内容</param>
        /// <param name="status">状态</param>
        /// <returns></returns>
        public ActionResult ListGetData(S_RMS_Config searchModel)
        {
            PageParams page = SetPageParams();
            var result_p = bll.GetByPage(page, searchModel);
            if (!result_p.IsSuccess)
            {
                return Json(result_p);
            }
            var result = SetPageJson(page, result_p.Data);
            return Json(result);
        }
        /// <summary>
        /// 删除和批量删除
        /// </summary>
        /// <param name="id">Id字符串","分隔</param>
        /// <returns></returns>
        public ActionResult Delete(string id)
        {
            var result = bll.Delete(id);
            return Json(result);
        }
        /// <summary>
        /// 编辑页
        /// </summary>
        /// <param name="id">数据Id</param>
        /// <param name="parentId">父节点Id</param>
        /// <param name="programId">项目Id</param>
        /// <returns></returns>
        public ActionResult Edit(string id, string parentId, string programId)
        {
            RMS_Config_Extend model = new RMS_Config_Extend();
            if (!string.IsNullOrWhiteSpace(id))
            {
                var result = bll.GetById(id);
                if (!result.IsSuccess)
                {
                    ViewBag.ErrorMsg = result.Message;
                }
                model = result.Data;
            }
            return PartialView(model);
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="model">编辑后的实体</param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Edit(RMS_Config_Extend model_e)
        {
            var result = new CommonOutPutVm();
            #region 模型验证
            var errmsg = ModelState.CheckHtmlErrorMsg();
            if (!string.IsNullOrWhiteSpace(errmsg))
            {
                result.Message = errmsg;
                return Json(result);
            } 
            #endregion
            #region 赋值并保存
            if (string.IsNullOrWhiteSpace(model_e.Id))
            {
                model_e.Id = Guid.NewGuid().ToString();
                model_e.EDeleted = E_Deleted.Normal;
                model_e.CreationTime = DateTime.Now;
                model_e.CreationUser = UserId;
                model_e.LastUpdateTime = DateTime.Now;
                model_e.LastUpdateUser = UserId;
                result = bll.Add(model_e);
            }
            else
            {
                model_e.LastUpdateTime = DateTime.Now;
                model_e.LastUpdateUser = UserId;
                result = bll.Updata(model_e);
            }
            #endregion
            return Json(result);
        }
    }
}